/*
 * @Author: huangjian mentu@ttigd.com
 * @Date: 2024-07-04 14:19:51
 * @LastEditors: huangjian mentu@ttigd.com
 * @LastEditTime: 2024-07-04 19:44:22
 * @FilePath: \cocosQRCodeProject\assets\scripts\QRTest.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { _decorator, Color, Component, EditBox, Graphics, ImageAsset, Node, Sprite, SpriteFrame, Texture2D, UITransform } from 'cc';
import { QRCode } from './QRcode/QRCode';
const { ccclass, property } = _decorator;

@ccclass('QRTest')
export class QRImgTest extends Component {

    @property(Sprite)
    qrcodeImg: Sprite = null;

    @property(EditBox)
    editBox: EditBox = null;
    start() {

    }

    update(deltaTime: number) {
        
    }

    /**
     * cocoscreator2.x
     */
    // private QRCreateImgByBase64(base64: string) {
    //     let image = new Image;
    //     image.onload = () => {
    //         let texture = new cc.Texture2D();
    //         texture.initWithElement(image);
    //         texture.handleLoadedTexture();
    //         this.qrcodeImg.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(texture);
    //     }
    //     image.src = 'data:image/png;base64,' + base64;
    // }

    /**
     * cocoscreator3.x
     */
    private QRCreateImgByBase64() {
        let base64 = this.editBox.string;
        const img = new Image();
       
        img.onload = () => {
            const texture = new Texture2D();
            texture.image =  new ImageAsset(img);
            const spriteFrame = new SpriteFrame();
            spriteFrame.texture = texture;
            this.qrcodeImg.spriteFrame = spriteFrame;
        };
        img.src = "data:image/png;base64," + base64;
    }
}


